<template>
  <div>
    <div>
      <!--      <el-row v-show="showSearch">-->
      <!--        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">-->
      <!--      <el-form-item label="对应的npk某一时刻检测的数据ID" prop="kgnpkDevDataId" >-->
      <!--        <el-input placeholder="请输入对应的npk某一时刻检测的数据ID" v-model="state.queryForm.kgnpkDevDataId" />-->
      <!--      </el-form-item>-->
      <!--          <el-form-item>-->
      <!--            <el-button icon="search" type="primary" @click="getDataList">-->
      <!--              查询-->
      <!--            </el-button>-->
      <!--            <el-button icon="Refresh" @click="resetQuery">重置</el-button>-->
      <!--          </el-form-item>-->
      <!--        </el-form>-->
      <!--      </el-row>-->
      <el-row class="add_el_row_wrapper" style="background: white;margin: 0;padding-bottom: 18px;padding-left: 10px">
        <el-col :span="15" class="add_el_button_el_col"></el-col>
        <el-col :span="9" class="right_tool_el_col">
          <div style="margin-right: 10px"></div>
          <right-toolbar v-model:showSearch="showSearch" :export="'duorou_duorouKgnpkDevDataInfo_export'"
                         @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
                         @queryTable="getDataList"></right-toolbar>
        </el-col>
      </el-row>
<!--      原始样式-->
      <el-row v-if="false">
        <div class="mb8" style="width: 100%">
          <right-toolbar v-model:showSearch="showSearch" :export="'duorou_duorouKgnpkDevDataInfo_export'"
                         @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
                         @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading"
                :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
                @selection-change="selectionChangHandle"
                @sort-change="sortChangeHandle">

<!--        <el-table-column type="selection" width="40" align="center"/>-->

        <el-table-column type="index" label="#" width="40"/>
        <el-table-column prop="kgnpkDevDataId" label="数据ID" show-overflow-tooltip/>
        <el-table-column prop="measureName" label="测量参数" show-overflow-tooltip/>
        <el-table-column prop="measureValue" label="测量值" show-overflow-tooltip/>
        <el-table-column prop="measureUnit" label="单位" show-overflow-tooltip/>
        <el-table-column prop="measureRawValue" label="测量原始值" show-overflow-tooltip/>
        <el-table-column prop="dataUnit" label="数据" show-overflow-tooltip/>
        <el-table-column prop="measureRange" label="量程" show-overflow-tooltip/>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)"/>

  </div>
</template>

<script setup lang="ts" name="systemDuorouKgnpkDevDataInfo">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {fetchList, delObjs} from "/@/api/duorou/duorouKgnpkDevDataInfo";
import {useMessage, useMessageBox} from "/@/hooks/message";
import {useDict} from '/@/hooks/dict';

// const emit = defineEmits(['refresh']);

/**
 * 定义组件属性 接收传递来的 devDataId表示查询哪一天的土壤检测数据
 */
const props = defineProps({
  duorouKgnpkDevData: {
    type: Object,
    default: () => null,
  },
  iotDevice: {
    type:Object,
    default: () => {},
  },
})


// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {
    kgnpkDevDataId: props.duorouKgnpkDevData?.id? props.duorouKgnpkDevData.id : null , //设置某一时刻的数据ID
  },
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []

  getLatestData()

}

// 导出excel
const exportExcel = () => {
  downBlobFile('/duorou/duorouKgnpkDevDataInfo/export', Object.assign(state.queryForm, {ids: selectObjs}), 'duorouKgnpkDevDataInfo.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({id}) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};



import {fetchList as fetchDevDataList} from "/@/api/duorou/duorouKgnpkDevData";

/**
 * 检测是不是获取 最新的数据
 */
function getLatestData(){
  if(props.duorouKgnpkDevData==null && props.iotDevice){
    const queryForm= {
      iotDeviceId:props.iotDevice.id,
      current: 1,
      size: 1,
    }
    return fetchDevDataList(queryForm).then(res=>{
      state.queryForm.kgnpkDevDataId=res.data.records.length>0 ? res.data.records[0].id : null
    }).then(()=>{
      getDataList()
    })
  }
}
getLatestData()
</script>